package com.teamwork.module03.controller.publish;

import com.teamwork.common.result.Result;
import com.teamwork.module03.service.file.UploadService;
import com.teamwork.module03.service.publish.PublishService;
import com.teamwork.module03.util.FileUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.parameters.P;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Map;

@RestController
@RequestMapping("/api/publish")
@Slf4j
public class PublishController {

    @Resource
    private PublishService publishService;

    //内容图片上传路径
    @Value("${article.imgPath}")
    private String imgPath;

    //用户发布文章
    @PostMapping("/{userid}")
    public Result publishArticle(@PathVariable("userid")Integer userid, MultipartFile cover,
                                 @Param("title")String title, @Param("content")String content) throws IOException {


         //  log.info("userid:{},covers:{}",userid,cover.getOriginalFilename());
          // log.info("title:{},content:{}",title,content);


       return  publishService.publish(userid,title,content,cover);

    }

    //删除用户取消的图片
    @DeleteMapping("/delete/{imgname}")
    public Result removeImg(@PathVariable("imgname")String imgname) throws FileNotFoundException {

        boolean b = FileUtils.DeleteFile(imgname, imgPath);
        if(!b){
            log.error("富文本图片删除失败！！！");
            return Result.failure("图片删除失败！");
        }

        return Result.success("图片删除成功！");
    }

}
